﻿using System;
using System.Web;
using System.IO;
using System.Threading;
using System.Text;
using Mobile.Wurfl.BrowserUpdater;
using System.Runtime.Remoting.Messaging;
using System.Net;
using System.Xml;


namespace Mobile.Urfl.BrowserUpdater
{
    public class WurflAppBrowserUpdater : IHttpModule
    {
        //private FileStream _file;
        //private static long _position = 0;
        private static object _lock = new object();

        public void Init(HttpApplication application)
        {
            application.AddOnPreRequestHandlerExecuteAsync(
                new BeginEventHandler(BeginPreRequestHandlerExecute),
                new EndEventHandler(EndPreRequestHandlerExecute)
            );
        }


        // define a delegate for the long operation
        delegate void CacheFlusher(HttpApplication app);

        IAsyncResult BeginPreRequestHandlerExecute(Object source,
            EventArgs e, AsyncCallback cb, Object state)
        {
            HttpApplication app = (HttpApplication)source;

            WUrlfUpdater updater = new WUrlfUpdater();
            CacheFlusher c = new CacheFlusher(updater.ExecuteUpdate);

                return c.BeginInvoke(app, cb, state);
        }

        void EndPreRequestHandlerExecute(IAsyncResult ar)
        {
            //any cleanup?
        }

        public void Dispose() { }

    }
}


